package 第三章作业;

import java.io.*;
import java.util.Scanner;

public class Work2 {
    static int j=-1;
    public static void main(String[] args) throws IOException {
        File file=new File("F:\\java_senior_collection\\Worldcup.txt");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Writer writer=null;
        BufferedWriter bw=null;
       String[] strs=new String[10];
        Scanner input =new Scanner(System.in);
        System.out.println("请输入一个年份");
        String year=input.next();
        try {
            writer=new FileWriter("F:\\java_senior_collection\\Worldcup.txt");
            bw=new BufferedWriter(writer);
            bw.write("1998法国");
            bw.newLine();
            bw.write("2002巴西");
            bw.newLine();
            bw.write("2006意大利");
            bw.newLine();
            bw.write("2010西班牙");
            bw.newLine();
            bw.write("2014德国");
            bw.newLine();
            bw.flush();
            FileInputStream fis=new FileInputStream("F:\\java_senior_collection\\Worldcup.txt");
            Reader r=new InputStreamReader(fis,"UTF-8");
            BufferedReader br=new BufferedReader(r);
            String line=null;
            while ((line=br.readLine())!=null) {
                strs[++j] = line;
            }
                for (String s:strs){
                    if (s.contains(year)){
                        String country=s.substring(4,s.length());
                        System.out.println("这一年的世界冠军是："+country);
                    }
                }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
                bw.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

